//
//  ForgetterViewController.m
//  mytext
//
//  Created by wj on 16/6/6.
//  Copyright © 2016年 wj. All rights reserved.
//

#import "ForgetterViewController.h"
#import "forgetNextViewController.h"

@interface ForgetterViewController ()
{
    int _secondsCountDown;
    NSTimer* _countDownTimer;
}
@property (nonatomic ,strong)UIImageView *imageV2;
@end

@implementation ForgetterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _secondsCountDown = 60;
    self.yzmT2.delegate = self;
    
    [self setUI];
//    [self getTuXingMakeCode];
}

-(void)timenFireMethod
{
    _secondsCountDown--;
    if (_secondsCountDown > 0)
    {
        [self.checkBtn setTitle:[NSString stringWithFormat:@"%d s",_secondsCountDown] forState:UIControlStateNormal];
        self.checkBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    else
    {
        [_countDownTimer invalidate];
        _countDownTimer = nil;
        _secondsCountDown = 60;
        [self.checkBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        self.checkBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.checkBtn setEnabled:YES];
    }
    
    NSLog(@"%d",_secondsCountDown);
}

- (void)setUI
{
//    UIView *leftV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 1)];
//    self.userNameT.leftView = leftV;
//    self.userNameT.leftViewMode = UITextFieldViewModeAlways;
//
//    
//    UIView *leftV1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 1)];
//    self.yzmT1.leftView = leftV1;
//    self.yzmT1.leftViewMode = UITextFieldViewModeAlways;
//
//    
//    UIView *leftV2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11, 1)];
//    self.yzmT2.leftView = leftV2;
//    self.yzmT2.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20,34, 10, 20)];
    imageView.image = [UIImage imageNamed:@"back-arrow"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:imageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    [imageView addGestureRecognizer:tap];

    self.title = @"取回密码";


    self.checkBtn.layer.cornerRadius = 5;

    
//    UIView *rightV1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11+14, 34)];
//    UIImageView *imageV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 14, 14)];
//    imageV1.image = [UIImage imageNamed:@"register_correct"];
//    [rightV1 addSubview:imageV1];
//    self.yzmT1.rightView = rightV1;
//    self.yzmT1.rightViewMode = UITextFieldViewModeAlways;
    
//    UIView *rightV2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 11+14, 34)];
//    _imageV2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 14, 14)];
//    _imageV2.image = [UIImage imageNamed:@"register_error"];
//    [rightV2 addSubview:_imageV2];
//    self.yzmT2.rightView = rightV2;
//    self.yzmT2.rightViewMode = UITextFieldViewModeAlways;
    
    
    
    self.userTop.constant = [FrameTool getToolY:99];
    self.yzmTop.constant = [FrameTool getToolY:99];
    self.checkBtnTop.constant = [FrameTool getToolY:124];
    
    self.Btn.layer.cornerRadius = 5;
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getTuXingMakeCode)];
    self.yzmTX.userInteractionEnabled = YES;
    [self.yzmTX addGestureRecognizer:tap1];
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)getYzm:(id)sender
{
    if ([self valiMobile:self.userNameT.text] == nil)
    {
        NSDictionary* dic = @{@"mobile":self.userNameT.text};
        [[LJFNetworkingManager shareManager] POST:GETPASSWORDBACK_URL parameters:dic success:^(id returnData, int code, NSString *msg)
         {
             if (code == 1)
             {
                 [self.checkBtn setEnabled:NO];
                 _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timenFireMethod) userInfo:nil repeats:YES];
                 
                 NSLog(@"msg == %@",msg);
                 
             }else
             {
                 NSLog(@"msg == %@",msg);
                 [SVProgressHUD showErrorWithStatus:msg];
             }
         } failure:^(NSError *error)
         {
             
         } showHUD:self.view];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:[self valiMobile:self.yzmT1.text]];
    }
}

- (IBAction)sendYzm:(id)sender
{
    BOOL result = [self.yzmT2.text compare:self.yzmTX.text
                                   options:NSCaseInsensitiveSearch | NSNumericSearch]== NSOrderedSame;
    
    if ([self valiMobile:self.userNameT.text] != nil) {
        [SVProgressHUD showErrorWithStatus:[self valiMobile:self.yzmT1.text]];
        return;
    }
//    if (self.yzmT1.text.length == 0) {
//        [SVProgressHUD showErrorWithStatus:@"请输入短信验证码"];
//        return;
//    }
//    if (self.yzmT2.text.length == 0) {
//        [SVProgressHUD showErrorWithStatus:@"请输入图形验证码"];
//        return;
//    }
//    if (!result)
//    {
//        [SVProgressHUD showErrorWithStatus:@"图形验证码输入错误"];
//        return;
//    }
    
//    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
//    [dictM setValue:self.yzmT2.text forKey:@"captcha"];
//    [dictM setValue:self.yzmT1.text forKey:@"verify_code"];
//    [dictM setValue:self.userNameT.text forKey:@"mobile"];
    
//    [[LJFNetworkingManager shareManager] POST:CHECKURL parameters:dictM success:^(id returnData, int code, NSString *msg)
//     {
//         if (code == 200)
//         {
//             forgetNextViewController *forgetNextVC = [[forgetNextViewController alloc] init];
//             forgetNextVC.mobile = self.userNameT.text;
//             
//             [self.navigationController pushViewController:forgetNextVC animated:YES];
//             
//             [_countDownTimer invalidate];
//             _countDownTimer = nil;
//             NSLog(@"msg == %@",msg);
//         }
//         else
//         {
//             NSLog(@"msg == %@",msg);
//             [SVProgressHUD showErrorWithStatus:msg ];
//         }
//     } failure:^(NSError *error)
//     {
//         
//     } showHUD:self.view]; 
}

//#pragma mark - 图形验证码
//- (void)getTuXingMakeCode
//{
//    [[LJFNetworkingManager shareManager] POST:TUXINGURL parameters:nil success:^(id returnData, int code, NSString *msg)
//     {
//         if (code == 200)
//         {
//             NSLog(@"msg == %@",msg);
//             
//             self.yzmTX.text = returnData;
//         }else
//         {
//             NSLog(@"msg == %@",msg);
//             [SVProgressHUD showErrorWithStatus:msg ];
//         }
//     } failure:^(NSError *error)
//     {
//         
//     } showHUD:nil];
//    
//}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.yzmT2)
    {
        BOOL result = [self.yzmT2.text compare:self.yzmTX.text
                                        options:NSCaseInsensitiveSearch | NSNumericSearch]== NSOrderedSame;
        if (result)
        {
            _imageV2.image = [UIImage imageNamed:@"register_correct"];
        }
        else
        {
            _imageV2.image = [UIImage imageNamed:@"register_error"];
        }
    }
}

- (NSString *)valiMobile:(NSString *)mobile{
    if (mobile.length < 11)
    {
        return @"手机号长度不是11位";
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return nil;
        }else{
            return @"请输入正确的*****号码";
        }
    }
    return nil;
}

@end
